<template>
  <div class="dashboard-container">
    <div class="header">
      <el-input
        v-model="input"
        clearable
        placeholder="请输入员工姓名"
        @keyup.native.enter="init"
      >
        <i slot="prefix" class="el-input__icon el-icon-search" @click="init"></i
      ></el-input>
      <el-button
        icon="el-icon-plus"
        @click="$router.push({ name: 'AddEmployee', query: { isEdit: 0 } })"
        >添加</el-button
      >
    </div>
    <el-table :data="tableData" style="width: 100%">
      <el-table-column label="员工姓名" prop="name"> </el-table-column>
      <el-table-column label="账号" prop="username"> </el-table-column>
      <el-table-column label="手机号" prop="phone"> </el-table-column>
      <el-table-column label="账号状态" prop="status">
        <template slot-scope="scope"
          >{{ scope.row.status ? "正常" : "已禁用" }}
        </template>
      </el-table-column>
      <el-table-column align="right">
        <template slot-scope="scope">
          <el-link type="primary">编辑</el-link>
          <el-divider direction="vertical"></el-divider>
          <el-popconfirm
            title="确认调整该账号的状态?"
            @onConfirm="handleChangeStatus(scope.row)"
          >
            <el-link type="danger" slot="reference">{{
              scope.row.status ? "禁用" : "启用"
            }}</el-link>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pageInfo.page"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="pageInfo.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import { getEmployees, changeStatus } from "@/api/employee";

export default {
  name: "Dashboard",
  computed: {
    ...mapGetters(["name"]),
  },
  data() {
    return {
      pageInfo: {
        page: 1,
        pageSize: 10,
      },
      input: "",
      tableData: [],
      total: 0,
      currentPage4: 4,
    };
  },
  created() {
    this.init();
  },

  methods: {
    init() {
      getEmployees({ ...this.pageInfo, name: this.input }).then((res) => {
        const { records, total } = res.data;
        console.log(total);

        this.tableData = records;
        this.total = total;
      });
    },
    async handleChangeStatus(row) {
      await changeStatus({ id: row.id, status: +!row.status });
      this.$message.success("状态改变成功");
      this.init();
    },
    handleSizeChange(val) {
      this.pageInfo.pageSize = val;
      this.init();
    },
    handleCurrentChange(val) {
      this.pageInfo.page = val;
      this.init();
    },
  },
};
</script>

<style lang="scss" scoped>
.dashboard {
  &-container {
    margin: 30px;
    .header {
      display: flex;
      justify-content: space-between;
      .el-input {
        width: 250px;
      }
    }
  }
  &-text {
    font-size: 30px;
    line-height: 46px;
  }
}
</style>
